home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / GNULicense / GNULicense.m < prev    next >
Text File  |  1995-06-12  |  8KB  |  303 lines

  1. // Copyright (C) 1989 Jacob Gore
  2. // 
  3. // This file is part of GNULicense, a package to ease the inclusion
  4. // of the GNU General Public License into NextStep applications.
  5. // 
  6. //     This program is free software; you can redistribute it and/or modify
  7. //     it under the terms of the GNU General Public License as published by
  8. //     the Free Software Foundation; either version 1, or (at your option)
  9. //     any later version.
  10. // 
  11. //     This program is distributed in the hope that it will be useful,
  12. //     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. //     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. //     GNU General Public License for more details.
  15. // 
  16. //     You should have received a copy of the GNU General Public License
  17. //     along with this program; if not, write to the Free Software
  18. //     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA
  19. //     or send electronic mail to Jacob Gore <jacob@gore.com>.
  20.  
  21. // The code to print the License leaves much to be desired.  I have left my
  22. // most successful attempts at providing the user with the printing interface
  23. // that is supposed to be the norm on the NeXT (put up a PrintPanel, etc.) in
  24. // this file, commented out.  If anybody can do it right, please send me the
  25. // code!
  26. //
  27. // Jacob Gore <jacob@gore.com>
  28.  
  29. #import "GNULicense.h"
  30. #import <appkit/appkit.h>
  31. #import <appkit/Application.h>
  32. #import <appkit/Panel.h>
  33. #import <appkit/PrintPanel.h>
  34. #import <appkit/PrintInfo.h>
  35. #import <appkit/Speaker.h>
  36. #import <appkit/TextField.h>
  37. #import <stdio.h>
  38. #import <string.h>
  39. #import <stdlib.h>
  40. #import <syslog.h>
  41.  
  42. @implementation GNULicense
  43.  
  44. + new
  45. {
  46.     self = [super new];
  47.     [NXApp loadNibSection:"GNULicense.nib" owner:self];
  48.     [panel setMiniwindowIcon:"app"];
  49.     return self;
  50. }
  51.  
  52. - showLicense:sender
  53. {
  54.     [panel makeKeyAndOrderFront:sender];
  55.     return self;
  56. }
  57.  
  58. // The original intention of this version of -printLicense: was to let
  59. // WriteNow print it.  I was hoping that I could just send -msgPrint:ok: to
  60. // port "WriteNow", and WriteNow would either just print it with default
  61. // user choices, or, better yet, put up a PrintPanel for the user.
  62. //
  63. // It appears that WriteNow does not respond to -msgPrint:ok: at all...
  64. // so, I just make WriteNow open the file, and I ask the user to do the
  65. // printing from WriteNow.  Yuck.
  66. // -- Jacob
  67. //
  68. - (int)printLicenseFile:(const char*)fullFileName
  69. {
  70.     int retCode;
  71.     int ok = (int)NO;
  72.  
  73.     [NXApp deactivateSelf];
  74.     retCode = [speaker openFile:fullFileName ok:&ok];
  75.     if (retCode != 0) {
  76.     NXRunAlertPanel(NULL, "Cannot connect to WriteNow",
  77.             "Cancel", NULL, NULL);
  78.     return -2;    // Tell NXFilePathSearch to quit at this point
  79.     } else if (!ok) {
  80.     return 1;    // Tell NXFilePathSearch to continue searching
  81.     } else {
  82.     return -2;    // Tell NXFilePathSearch to quit at this point
  83.     }
  84. }
  85.  
  86. static int
  87. printLicenseFile(const char *fullFileName, void *self)
  88. {
  89.     return [(GNULicense*)self printLicenseFile:fullFileName];
  90. }
  91.  
  92. - printLicense:sender
  93. {
  94.     int response;
  95.     response = NXRunAlertPanel("Sorry to bother you",
  96.                    "I will make WriteNow open the License "
  97.                    "document. Please print it from there.",
  98.                    NULL, "Cancel", NULL);
  99.     if (response == NX_ALERTDEFAULT) {
  100.  
  101.     int port = NXPortFromName("WriteNow", NULL);
  102.  
  103.     if (port == PORT_NULL) {
  104.         NXRunAlertPanel(NULL, "Cannot connect to WriteNow",
  105.                 "Cancel", NULL, NULL);
  106.     } else {
  107.  
  108.         const char *searchPath =
  109.           "~/Library/GNU:/LocalLibrary/GNU:/NextLibrary/GNU:.";
  110.         const char *fileName = "GNULicense.wn";
  111.         int retCode;
  112.  
  113.         speaker = [Speaker new];
  114.         [speaker setSendPort:port];
  115.  
  116.         retCode = NXFilePathSearch(NULL, searchPath, YES, fileName,
  117.                        printLicenseFile, (void*)self);
  118.         if (retCode != -2) {
  119.         syslog(LOG_ERR,
  120.                "Readable file \"%s\" not found in path \"%s\"",
  121.                fileName, searchPath);
  122.         NXRunAlertPanel(NULL, "I cannot find file GNULicense.wn.",
  123.                 "Cancel", NULL, NULL);
  124.         }
  125.     }
  126.     }
  127.     return self;
  128. }
  129.  
  130. // Could not get this version to work:
  131. //
  132. //   1.    Save button on PrintPanel does not return to this code, but instead
  133. //    saves "what has been spooled".  I cannot figure out how to make the
  134. //    postscript file to be the "what has been spooled".
  135. //
  136. //   2.    If you select a page range to preview, Preview chokes on the output
  137. //    of psrev that it gets.
  138. //
  139. // Close, but no banana.
  140. // -- Jacob
  141. //
  142. // - printLicense:sender
  143. // {
  144. //     int pickedButtonTag;
  145. //     PrintPanel *printPanel;
  146. // 
  147. //     printPanel = [PrintPanel new];
  148. //     pickedButtonTag = [printPanel runModal];
  149. //     [printPanel orderOut:self];
  150. // 
  151. //     if (pickedButtonTag == NX_CANCELTAG) {
  152. // 
  153. //     return self;
  154. // 
  155. //     } else {
  156. // 
  157. //     const char *fileName = "/LocalLibrary/GNU/GNULicense.ps";
  158. //     char spoolFileName[] = "/tmp/GNULicenseXXXXXX.ps";
  159. //     PrintInfo *printInfo;
  160. //     const char *psrevCommandFormat = "psrev%s%s %s > %s";
  161. //     char *psrevCommand;
  162. //     BOOL allPages;
  163. //     char psrevPagesOption[32];
  164. //     char *psrevReverseOption;
  165. // 
  166. //     // Use psrev to create a spool file
  167. // 
  168. //     printInfo = [NXApp printInfo];
  169. // 
  170. //     if ([printInfo isAllPages]) {
  171. //         psrevPagesOption[0] = '\0';
  172. //     } else {
  173. //         sprintf(psrevPagesOption, " -s%d-%d",
  174. //             [printInfo firstPage], [printInfo lastPage]);
  175. //     }
  176. // 
  177. //     if ([printInfo pageOrder] == NX_DESCENDINGORDER) {
  178. //         psrevReverseOption = "";    // psrev reversed by default
  179. //     } else {
  180. //         psrevReverseOption = " -R";
  181. //     }
  182. // 
  183. //     NXGetTempFilename(spoolFileName,
  184. //               strchr(spoolFileName, 'X')-spoolFileName);
  185. // 
  186. //     psrevCommand = malloc(strlen(psrevCommandFormat)-8 +
  187. //                   strlen(psrevPagesOption) +
  188. //                   strlen(psrevReverseOption) +
  189. //                   strlen(fileName) +
  190. //                   strlen(spoolFileName) +
  191. //                   1);
  192. // 
  193. //     if (psrevCommand) {
  194. //         int result;
  195. //         sprintf(psrevCommand, psrevCommandFormat,
  196. //             psrevPagesOption, psrevReverseOption,
  197. //             fileName, spoolFileName);
  198. // 
  199. //         result = system(psrevCommand);
  200. //         if (result != 0) {
  201. //         // ???
  202. //         }
  203. //         free(psrevCommand);
  204. //     } else {
  205. //         // ???
  206. //     }
  207. // 
  208. //     [printInfo setOutputFile:spoolFileName];
  209. // 
  210. //     // Now do something with the spool file
  211. // 
  212. //     switch (pickedButtonTag) {
  213. //       case NX_PREVIEWTAG:
  214. //         {
  215. //         port_t port;
  216. //         int openedOk;
  217. // 
  218. //         port = NXPortFromName("Preview", NULL);
  219. //         if (port == PORT_NULL) {
  220. //             // ???
  221. //         } else {
  222. //             int retCode;
  223. //             Speaker *speaker = [Speaker new];
  224. //             [speaker setSendPort:port];
  225. //             retCode = [speaker openTempFile:spoolFileName
  226. //                                      ok:&openedOk];
  227. //             if (retCode != 0 || !openedOk) {
  228. //             // ???
  229. //             }
  230. //             [speaker free];
  231. //         }
  232. //         }
  233. //         break;
  234. //       case NX_OKTAG:
  235. //         {
  236. //         const char *printCommandFormat = "lpr -sr%s%s %s";
  237. //                       // "-sr" will delete the file after it is printed
  238. //         int copies;
  239. //         char *printCommand;
  240. //         char printCopiesOption[16];
  241. //         char *printPrinterOption;
  242. //         const char *printerName;
  243. // 
  244. //         copies = [printInfo copies];
  245. //         if (copies > 1) {
  246. //             sprintf(printCopiesOption, " -#%d", copies);
  247. //         } else {
  248. //             printCopiesOption[0] = '\0';
  249. //         }
  250. // 
  251. //         printerName = [printInfo printerName];
  252. //         if (printerName) {
  253. //             printPrinterOption = malloc(strlen(printerName) +
  254. //                         4 /* room for " -P" & '\0' */);
  255. //             sprintf(printPrinterOption, " -P%s", printerName);
  256. //         } else {
  257. //             printPrinterOption = malloc(1);    // so that free() works
  258. //             *printPrinterOption = '\0';
  259. //         }
  260. // 
  261. //         if (printerName == NULL) {
  262. //             // ???
  263. //         }
  264. // 
  265. //         printCommand = malloc(strlen(printCommandFormat)-6 +
  266. //                       strlen(printCopiesOption) +
  267. //                       strlen(printPrinterOption) +
  268. //                       strlen(spoolFileName) +
  269. //                       1);
  270. //         if (printCommand) {
  271. //             int result;
  272. //             sprintf(printCommand, printCommandFormat,
  273. //                 printCopiesOption, printPrinterOption,
  274. //                 spoolFileName);
  275. //             result = system(printCommand);
  276. //             if (result != 0) {
  277. //             // ???
  278. //             }
  279. //             free(printCommand);
  280. //         } else {
  281. //             // ???
  282. //         }
  283. //         }
  284. //         break;
  285. //       case NX_SAVETAG:
  286. //         {
  287. //         fprintf(stderr, "%s\n", [printInfo outputFile]);
  288. //         unlink(spoolFileName);
  289. //         }
  290. //         break;
  291. //     }
  292. //     }
  293. //     return self;
  294. // }
  295.  
  296. - setPanel:anObject
  297. {
  298.     panel = anObject;
  299.     return self;
  300. }
  301.  
  302. @end
  303.